package com.huluobo.lib_base

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import java.io.File
import java.lang.Exception

/**
 *  Created by LC on 2025/2/25.
 */
abstract class BaseViewModel<uiIntent : UIIntent> : ViewModel() {
    //存放管理意图的管道
    private val _uiIntent = Channel<UIIntent> { Channel.UNLIMITED }

    //可变的状态,用于更新UI
    private var _state = MutableStateFlow<UIState>(UIState.OnLoading)
    val state: StateFlow<UIState>
        get() = _state

    init {
        viewModelScope.launch {
            _uiIntent.receiveAsFlow().collect {
                handleIntent(it)
            }
        }
    }

    //所有继承当前BaseViewModel的类,都需要实现该方法,来处理对应的意图
    //比如登录的Intent,需要处理登录的意图
    abstract fun handleIntent(it: UIIntent)

    //发送意图
    fun sendIntent(intent: UIIntent) {
        viewModelScope.launch {
            _uiIntent.send(intent)
        }
    }

    fun httpRequest(request: Flow<Res<Any>>, type: HttpType = HttpType.DEFAULT) {
        viewModelScope.launch {
            request.flowOn(Dispatchers.IO)
                .catch {
                    Log.e("TagA", "error:${it.message}")
                }.collect {
                    if (it.code == 0) {
                        _state.value = UIState.OnSuccess(it.data, type)
                    } else {
                        _state.value = UIState.OnError(it.msg)
                    }
                }
        }
    }

    fun httpRequestVideo(request: Flow<Res2<Any>>, type: HttpType = HttpType.DEFAULT){
        viewModelScope.launch {
            request.flowOn(Dispatchers.IO)
                .catch {
                    Log.e("TagA", "error:${it.message}")
                }.collect {
                    if (it.code == 200) {
                        _state.value = UIState.OnSuccess(it.data, type)
                    } else {
                        _state.value = UIState.OnError(it.message)
                    }
                }
        }
    }

    fun download(url: String, file: File) {
        viewModelScope.launch {
            DownloadUtil.download(url, file)
                .flowOn(Dispatchers.IO)
                .catch {
                    Log.e("TagA", "downError:${it.message}")
                }.collect {
                    _state.value = it
                }
        }
    }

    fun scanFile(file: File) {
        viewModelScope.launch {
            try {
                //查找文件夹中所有的文件,形成一个list
                val files = file.listFiles()
                val list = mutableListOf<LocalEntity>()
                for (it in files) {
                    list.add(LocalEntity(it.name, it.absolutePath))
                }
                _state.value = UIState.OnSuccess(list)

            } catch (e: Exception) {
                _state.value = UIState.OnError(e.message.toString())
            }
        }
    }

    fun roomRequest(request: Flow<Any>, type: HttpType = HttpType.DEFAULT) {
        viewModelScope.launch {
            request.flowOn(Dispatchers.IO)
                .catch {
                    Log.e("TagA", "数据库错误:${it.message.toString()}")
                }.collect {
                    if (it != null) {
                        _state.value = UIState.OnSuccess(it, type)
                    } else {
                        _state.value = UIState.OnError("数据库操作错误$it")
                    }
                }
        }
    }
}